Proper connect_port
[juce-lv2.git] / juce / source / extras / the jucer / src / model / components / jucer_ButtonHandler.h
blobcb3a42dc95cb1046e6e1fbf10f3b6e21c940319c
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_BUTTONHANDLER_JUCEHEADER__
27 #define __JUCER_BUTTONHANDLER_JUCEHEADER__
29 #include "../../properties/jucer_ComponentChoiceProperty.h"
30 #include "../../properties/jucer_ComponentTextProperty.h"
31 #include "../../properties/jucer_ComponentBooleanProperty.h"
32 #include "jucer_ComponentUndoableAction.h"
35 //==============================================================================
36 /**
38 class ButtonHandler : public ComponentTypeHandler
40 public:
41 //==============================================================================
42 ButtonHandler (const String& typeDescription_,
43 const String& className_,
44 const std::type_info& componentClass,
45 const int defaultWidth_,
46 const int defaultHeight_)
47 : ComponentTypeHandler (typeDescription_, className_, componentClass,
48 defaultWidth_, defaultHeight_)
51 //==============================================================================
52 void getEditableProperties (Component* component, JucerDocument& document, Array <PropertyComponent*>& properties)
54 ComponentTypeHandler::getEditableProperties (component, document, properties);
56 Button* const b = dynamic_cast <Button*> (component);
58 properties.add (new ButtonTextProperty (b, document));
60 properties.add (new ButtonCallbackProperty (b, document));
62 properties.add (new ButtonRadioGroupProperty (b, document));
64 properties.add (new ButtonConnectedEdgeProperty ("connected left", Button::ConnectedOnLeft, b, document));
65 properties.add (new ButtonConnectedEdgeProperty ("connected right", Button::ConnectedOnRight, b, document));
66 properties.add (new ButtonConnectedEdgeProperty ("connected top", Button::ConnectedOnTop, b, document));
67 properties.add (new ButtonConnectedEdgeProperty ("connected bottom", Button::ConnectedOnBottom, b, document));
70 XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
72 Button* const b = dynamic_cast <Button*> (comp);
74 XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout);
75 e->setAttribute ("buttonText", b->getButtonText());
76 e->setAttribute ("connectedEdges", b->getConnectedEdgeFlags());
77 e->setAttribute ("needsCallback", needsButtonListener (b));
78 e->setAttribute ("radioGroupId", b->getRadioGroupId());
80 return e;
83 bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
85 Button* const b = dynamic_cast <Button*> (comp);
87 if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
88 return false;
90 b->setButtonText (xml.getStringAttribute ("buttonText", b->getButtonText()));
91 b->setConnectedEdges (xml.getIntAttribute ("connectedEdges", 0));
92 setNeedsButtonListener (b, xml.getBoolAttribute ("needsCallback", true));
93 b->setRadioGroupId (xml.getIntAttribute ("radioGroupId", 0));
95 return true;
98 const String getCreationParameters (Component* component)
100 return quotedString (component->getName());
103 void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
105 ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
107 Button* const b = dynamic_cast <Button*> (component);
109 if (b->getButtonText() != b->getName())
111 code.constructorCode
112 << memberVariableName << "->setButtonText ("
113 << quotedString (b->getButtonText()) << ");\n";
116 if (b->getConnectedEdgeFlags() != 0)
118 StringArray flags;
120 if (b->isConnectedOnLeft())
121 flags.add ("Button::ConnectedOnLeft");
123 if (b->isConnectedOnRight())
124 flags.add ("Button::ConnectedOnRight");
126 if (b->isConnectedOnTop())
127 flags.add ("Button::ConnectedOnTop");
129 if (b->isConnectedOnBottom())
130 flags.add ("Button::ConnectedOnBottom");
132 String s;
133 s << memberVariableName << "->setConnectedEdges ("
134 << flags.joinIntoString (" | ") << ");\n";
136 code.constructorCode += s;
139 if (b->getRadioGroupId() != 0)
140 code.constructorCode << memberVariableName << "->setRadioGroupId ("
141 << b->getRadioGroupId() << ");\n";
143 if (needsButtonListener (component))
144 code.constructorCode << memberVariableName << "->addListener (this);\n";
147 void fillInGeneratedCode (Component* component, GeneratedCode& code)
149 ComponentTypeHandler::fillInGeneratedCode (component, code);
151 if (needsButtonListener (component))
153 String& callback = code.getCallbackCode ("public ButtonListener",
154 "void",
155 "buttonClicked (Button* buttonThatWasClicked)",
156 true);
158 if (callback.isNotEmpty())
159 callback << "else ";
161 const String memberVariableName (code.document->getComponentLayout()->getComponentMemberVariableName (component));
162 const String userCodeComment ("UserButtonCode_" + memberVariableName);
164 callback
165 << "if (buttonThatWasClicked == " << memberVariableName
166 << ")\n{\n //[" << userCodeComment << "] -- add your button handler code here..\n //[/" << userCodeComment << "]\n}\n";
170 static bool needsButtonListener (Component* button)
172 return button->getProperties().getWithDefault ("generateListenerCallback", true);
175 static void setNeedsButtonListener (Component* button, const bool shouldDoCallback)
177 button->getProperties().set ("generateListenerCallback", shouldDoCallback);
180 private:
181 //==============================================================================
182 class ButtonTextProperty : public ComponentTextProperty <Button>
184 public:
185 ButtonTextProperty (Button* button_, JucerDocument& document_)
186 : ComponentTextProperty <Button> ("text", 100, false, button_, document_)
190 void setText (const String& newText)
192 document.perform (new ButtonTextChangeAction (component, *document.getComponentLayout(), newText),
193 "Change button text");
196 const String getText() const
198 return component->getButtonText();
201 private:
202 class ButtonTextChangeAction : public ComponentUndoableAction <Button>
204 public:
205 ButtonTextChangeAction (Button* const comp, ComponentLayout& layout, const String& newName_)
206 : ComponentUndoableAction <Button> (comp, layout),
207 newName (newName_)
209 oldName = comp->getButtonText();
212 bool perform()
214 showCorrectTab();
215 getComponent()->setButtonText (newName);
216 changed();
217 return true;
220 bool undo()
222 showCorrectTab();
223 getComponent()->setButtonText (oldName);
224 changed();
225 return true;
228 String newName, oldName;
232 class ButtonCallbackProperty : public ComponentBooleanProperty <Button>
234 public:
235 ButtonCallbackProperty (Button* button, JucerDocument& document)
236 : ComponentBooleanProperty <Button> ("callback", "Generate ButtonListener", "Generate ButtonListener", button, document)
240 void setState (bool newState)
242 document.perform (new ButtonCallbackChangeAction (component, *document.getComponentLayout(), newState),
243 "Change button callback");
246 bool getState() const { return needsButtonListener (component); }
248 private:
249 class ButtonCallbackChangeAction : public ComponentUndoableAction <Button>
251 public:
252 ButtonCallbackChangeAction (Button* const comp, ComponentLayout& layout, const bool newState_)
253 : ComponentUndoableAction <Button> (comp, layout),
254 newState (newState_)
256 oldState = needsButtonListener (comp);
259 bool perform()
261 showCorrectTab();
262 setNeedsButtonListener (getComponent(), newState);
263 changed();
264 return true;
267 bool undo()
269 showCorrectTab();
270 setNeedsButtonListener (getComponent(), oldState);
271 changed();
272 return true;
275 bool newState, oldState;
279 class ButtonRadioGroupProperty : public ComponentTextProperty <Button>
281 public:
282 ButtonRadioGroupProperty (Button* const button_, JucerDocument& document_)
283 : ComponentTextProperty <Button> ("radio group", 10, false, button_, document_)
287 void setText (const String& newText)
289 document.perform (new ButtonRadioGroupChangeAction (component, *document.getComponentLayout(), newText.getIntValue()),
290 "Change radio group ID");
293 const String getText() const
295 return String (component->getRadioGroupId());
298 private:
299 class ButtonRadioGroupChangeAction : public ComponentUndoableAction <Button>
301 public:
302 ButtonRadioGroupChangeAction (Button* const comp, ComponentLayout& layout, const int newId_)
303 : ComponentUndoableAction <Button> (comp, layout),
304 newId (newId_)
306 oldId = comp->getRadioGroupId();
309 bool perform()
311 showCorrectTab();
312 getComponent()->setRadioGroupId (newId);
313 changed();
314 return true;
317 bool undo()
319 showCorrectTab();
320 getComponent()->setRadioGroupId (oldId);
321 changed();
322 return true;
325 int newId, oldId;
329 class ButtonConnectedEdgeProperty : public ComponentBooleanProperty <Button>
331 public:
332 ButtonConnectedEdgeProperty (const String& name, const int flag_,
333 Button* button, JucerDocument& document)
334 : ComponentBooleanProperty <Button> (name, "Connected", "Connected", button, document),
335 flag (flag_)
339 void setState (bool newState)
341 document.perform (new ButtonConnectedChangeAction (component, *document.getComponentLayout(), flag, newState),
342 "Change button connected edges");
345 bool getState() const
347 return (component->getConnectedEdgeFlags() & flag) != 0;
350 private:
351 const int flag;
353 class ButtonConnectedChangeAction : public ComponentUndoableAction <Button>
355 public:
356 ButtonConnectedChangeAction (Button* const comp, ComponentLayout& layout, const int flag_, const bool newState_)
357 : ComponentUndoableAction <Button> (comp, layout),
358 flag (flag_),
359 newState (newState_)
361 oldState = ((comp->getConnectedEdgeFlags() & flag) != 0);
364 bool perform()
366 showCorrectTab();
368 if (newState)
369 getComponent()->setConnectedEdges (getComponent()->getConnectedEdgeFlags() | flag);
370 else
371 getComponent()->setConnectedEdges (getComponent()->getConnectedEdgeFlags() & ~flag);
373 changed();
374 return true;
377 bool undo()
379 showCorrectTab();
381 if (oldState)
382 getComponent()->setConnectedEdges (getComponent()->getConnectedEdgeFlags() | flag);
383 else
384 getComponent()->setConnectedEdges (getComponent()->getConnectedEdgeFlags() & ~flag);
386 changed();
387 return true;
390 const int flag;
391 bool newState, oldState;
397 #endif // __JUCER_BUTTONHANDLER_JUCEHEADER__